﻿using System;
using System.Windows;
using AvalonDock;
using LukasKopenec.LocalizationSupport;
using Notoric.ViewModel.Elements;
using Page = Notoric.ViewModel.Elements.Page;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Interaction logic for PagePanel.xaml
    /// </summary>
    public partial class PagePanel : DocumentContent
    {
        #region Constructor(s)

        /// <summary>
        /// Creates a new <see cref="PagePanel"/> instance.
        /// </summary>
        public PagePanel()
        {
            InitializeComponent();
        }

        #endregion

        #region Dependency Properties

        /// <summary>
        /// Identifies the Page dependency property.
        /// </summary>
        public static readonly DependencyProperty PageProperty = DependencyProperty.Register(
            "Page", typeof(Page), typeof(PagePanel),
            new PropertyMetadata(new PropertyChangedCallback(PageProperty_Changed)));

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the value of the Page dependency property changed.
        /// </summary>
        /// <param name="d"><see cref="DependencyObject"/> on which the value changed.</param>
        /// <param name="e">Event data</param>
        private static void PageProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PagePanel panel = (PagePanel)d;

            if (panel.Page != null)
            {
                panel.pageToolTip.Text = String.Format(
                    (string)LocalizationManager.GetValue("PagePanel.ToolTip"),
                    panel.Page.Number,
                    panel.Page.FindOwner<Score>().Name
                    );
            }
        }

        /// <summary>
        /// Occurs when the panel has been closed.
        /// </summary>
        /// <param name="sender">Object that has raised the event</param>
        /// <param name="e">Event data</param>
        private void parentControl_Closed(object sender, EventArgs e)
        {
            Page.ClearSelection();
            SetValue(PageProperty, DependencyProperty.UnsetValue);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the page associated with this page panel.
        /// This is a dependency property.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Page"/>
        /// </para>
        /// <para>
        /// The page that is crrently displayed in the page viewer.
        /// </para>
        /// </value>
        public Page Page
        {
            get { return (Page)GetValue(PageProperty); }
            set { SetValue(PageProperty, value); }
        }

        #endregion
    }
}
